﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages.IndicatorHistory
{
    public class HistoryAdd : PatientBasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected LabcheckInfo IndicatorData;
        protected IndicatorHistoryInfo IndicatorHistoryData;

        protected new void HandleCommon()
        {
            this.AddLocation("实验室指标记录", "/IndicatortHistory/Index.aspx");
            this.AddLocation("添加记", "/IndicatortHistory/HistoryAdd.aspx");
            this.SetTitle("添加实验室指标记录");

            int idcid = RequestTools.GetQueryInt("idcid", 0);
            if (idcid <= 0)
            {
                this.AddErrLine("无效的参数");
                return;
            }

            IndicatorData = LabcheckBLL.Instance.GetLabcheckInfo(idcid);
            if (IndicatorData.Id <= 0)
            {
                this.AddErrLine("请选择治疗");
                return;
            }

            if (IndicatorData.IsQuantified > 0)
            {
                //解析正常值字符串
                string[] normalValues = IndicatorData.NormalValues.Split('@');
                if (normalValues.Length >= 2)
                {
                    IndicatorData.NormalMinValue = Convert.ToDouble(normalValues[0]);
                    IndicatorData.NormalMaxValue = Convert.ToDouble(normalValues[1]);
                }
            }
        }

        protected new void HandlePost()
        {
            IndicatorHistoryInfo info = this.ValidateForm();
            if (info == null) return;
            bool succ = PatientIndicatorBLL.Instance.AddIndicatorHistory(info);
            if (succ)
            {
                this.AddMsgLine("添加成功");
            }
            else
            {
                this.AddErrLine("添加失败");
            }
            this.SetUrl("/IndicatorHistory/List.aspx?idcid=" + IndicatorData.Id);
            this.SetMetaRefresh();
        }

        protected new void HandleGet()
        {
        }

        protected IndicatorHistoryInfo ValidateForm()
        {
            IndicatorHistoryInfo info = new IndicatorHistoryInfo();

            //// 对时间进行判断
            //string value = RequestTools.GetFormString("startdate");
            //if (value == "")
            //{
            //    this.AddErrLine("请填写开始时间");
            //    return null;
            //}
            //DateTime date;
            //bool succ = DateTime.TryParse(value, out date);
            //if (succ == false)
            //{
            //    this.AddErrLine("错误的时间 " + value);
            //    return null;
            //}
            //info.StartDate = date;

            //info.UsePurpose = RequestTools.GetInt("usepurpose", -1);

            //value = RequestTools.GetFormString("enddate");
            //if (value == "")
            //{
            //    this.AddErrLine("请填写停止时间");
            //    return null;
            //}
            //succ = DateTime.TryParse(value, out date);
            //if (succ == false)
            //{
            //    this.AddErrLine("错误的时间 " + value);
            //    return null;
            //}
            //info.EndDate = date;

            ////对时间进行检查

            //info.UsePurpose = RequestTools.GetFormInt("usepurpose", -1);
            //if (info.UsePurpose > 3 || info.UsePurpose < 0)
            //{
            //    this.AddErrLine("请填写使用原因");
            //    return null;
            //}

            //info.StopReason = RequestTools.GetFormInt("stopreason", -1);
            //if (info.StopReason > 3 || info.StopReason < 0)
            //{
            //    this.AddErrLine("请填写停止原因");
            //    return null;
            //}

            //info.DataValue = RequestTools.GetFormDouble("dosage", -1.0);
            //if (info.DataValue < 0)
            //{
            //    this.AddErrLine("请填写正确的剂量 ");
            //    return null;
            //}

            //info.State = Constants.STOPED_USE;
            //info.UpdateDate = DateTime.Now;
            //info.TreatmentId = TrmData.Id;
            //info.PRId = PrData.Id;

            return info;
        }
    }
}